// pages/sells/sellsList/sellsList.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    btnStyle: ['#bf9368', '#3a3938', '#3a3938'],
    pageNum: '1', //分页
    resultData: [], //数据源
    imgURL: app.globalData.imgURL, //图片总路径
    url: 'orders/queryOrdersForUser',
    orderSData: [],
    showViewId: 2,
    noOrdersMsg: '未查询到订单信息~~',
    selectID: 1,
    userInfo: {},
    orderStatus: "0",
    openId: '',
  },
  showOrderDetail: function (e) {
    var that = this;
    // wx.navigateTo({
    //   url: '../myOrderDeatil/myOrderDeatil?id=' + e.currentTarget.id + "&openId=" + that.data.openId + "&type=" + that.data.customerType,
    // })
    let toUrl = "";
    if ("seller" == e.currentTarget.dataset.type){
      toUrl = '../../buys/buysDetail/buysDetail?openId=' + that.data.openId + "&id=" + e.currentTarget.dataset.buyid + "&showReleaseBtn=show&toReleaseType=seller" ;
    }else{
      toUrl = '../../buys/buysDetail/buysDetail?openId=' + that.data.openId + "&id=" + e.currentTarget.id + "&entrance=buyList&showReleaseBtn=show&toReleaseType=buyer";
    }
    console.log('我的订单点击详情',e)
    wx.navigateTo({
      url: toUrl
    })

  },
// queryCustomerType:function(){
//   var that = this;
//   app.post("customer/queryCustomer",{openId : that.data.openId}).then((res) =>{
//     that.setData({
//       customerType: res.resultData.customer.customer_type
//     })
//   }).catch((errMsg) =>{
//     console.log('fail');
//     console.log(errMsg + '失败');
//   })
// },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (wx.getStorageSync('userInfo')){
      that.setData({
        userInfo: wx.getStorageSync('userInfo'),//获取用户信息
        openId: wx.getStorageSync('userInfo').userId, //获取openId
        orderSData: [],
        pageNum: 1,
        isLogin: 1,
      })
      // that.queryCustomerType();
      
    }
  //  console.log("角色："+that.data.customerType)
  },

  //请求数据
  requestData: function () {
    var that = this;
    app.post(that.data.url, {
      pageNum: that.data.pageNum, status: that.data.orderStatus, openId: that.data.openId
    }).then((res) => {
      console.log('success');
      console.log(res);
      if (res.code == 0) {
        that.setData({
          orderSData: that.data.orderSData.concat(res.resultData),
        })

      } else {
        wx.showModal({
          title: '温馨提示',
          content: res.msg,
          showCancel: false,
        })
      }
      console.log("订单数量：" + this.data.orderSData.length);
      if (that.data.orderSData.length == 0) {
        that.setData({
          showViewId: 2,
        })
      } else {
        that.setData({
          showViewId: 1,
        })
      }
    }).catch((errMsg) => {
      console.log('fail');
      console.log(errMsg + '失败');
    })
  },
  typeChoose: function (e) {
    var that = this;
    console.log('切换 未完成订单，已完成订单');

    if (e.currentTarget.id == that.data.selectID) {
      return;
    }
    console.log(e.currentTarget.id)
    if (e.currentTarget.id == 1) {
      //未完成订单
      that.setData({
        btnStyle: ['#bf9368', '#3a3938', '#3a3938'],
        selectID: e.currentTarget.id,
        pageNum: '1', //分页
        orderSData: [], //列表数据
        orderStatus: "0",
      })
    } else if (e.currentTarget.id == 2) {
      //优惠促销
      that.setData({
        btnStyle: ['#3a3938', '#bf9368', '#3a3938'],
        selectID: e.currentTarget.id,
        pageNum: '1', //分页
        listData: [], //列表数据
        orderSData: [], //未完成订单列表
        orderStatus: '1',
      })
    }
    that.requestData();
  },
  res_goods_info: function (e) {

    wx.navigateTo({
      url: '../sellsReleaseInfo/sellsReleaseInfo?buyId=' + e.currentTarget.id,
    })
       
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.setData({
      listData: [], //列表数据
      orderSData: [], //未完成订单列表
    })
    that.requestData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      orderSData: [],
      pageNum: '1',//分页
      userInfo: wx.getStorageSync('userInfo'),//获取用户信息
    })
    wx.showNavigationBarLoading();
    setTimeout(function () {
      that.requestData();
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
    }, 2000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('加载更多');
    var that = this;
    that.data.pageNum++;
    console.log(that.data.pageNum);
    that.requestData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})